package com.study.mike.common.utils;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.Base64.Encoder;
import java.util.Base64.Decoder;


/**
 * @author kh
 */
public class Base64Util {
    // 加密
    public static String getBase64(String str) {
        byte[] b = null;
        String s = null;
        Encoder encoder = Base64.getEncoder();
        try {
            b = str.getBytes("utf-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        if (b != null) {
            s = encoder.encodeToString(b);
        }
        return s;
    }

    // 解密
    public static String getFromBase64(String s) {
        byte[] b = null;
        String result = null;
        Decoder decoder = Base64.getDecoder();
        if (s != null) {
            try {
                b = decoder.decode(s);
                result = new String(b, "utf-8");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return result;
    }

//    public static void main(String[] args) {
//        String a = "123456";
//        String b = getBase64(a);
//        System.out.println(b);
//        System.out.println(getBase64(a));
//        System.out.println(getFromBase64(b));
//    }
}
